package com.zhoug.zmui3.dialog.gender;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.zhoug.zmui3.R;
import com.zhoug.zmui3.ZMUIAnimations;
import com.zhoug.zmui3.dialog.ZMUIBaseDialog;

import androidx.annotation.NonNull;


/**
 * 性别选择器
 */
public class ZMUIGenderChooser extends ZMUIBaseDialog {
    /**
     * 男
     */
    public static final int GENDER_MALE = 1;
    /**
     * 女
     */
    public static final int GENDER_FEMALE = 2;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private int gender = GENDER_MALE;
    private Callback mCallback;

    public ZMUIGenderChooser(@NonNull Context context) {
        this(context, 0);
    }

    public ZMUIGenderChooser(@NonNull Context context, int themeResId) {
        this(context, themeResId, 0, 0);
    }

    protected ZMUIGenderChooser(@NonNull Context context, int themeResId, int defStyleAttr, int defStyleRes) {
        super(context, themeResId, defStyleAttr, defStyleRes);

    }

    @Override
    protected void initialize(@NonNull Context context, int defStyleAttr, int defStyleRes) {
        super.initialize(context, defStyleAttr, defStyleRes);
        setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        setWindowAnimations(ZMUIAnimations.ANIM_DOWN);
        setFullWidth(true);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.zmui_gender_dialog;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mRadioButton1 = findViewById(R.id.gender_rb1);
        mRadioButton2 = findViewById(R.id.gender_rb2);
        LinearLayout mll1 = findViewById(R.id.gender_ll_item1);
        LinearLayout mll2 = findViewById(R.id.gender_ll_item2);
        setWindowAnimations(ZMUIAnimations.ANIM_DOWN);
        mll1.setOnClickListener(onClickListener);
        mll2.setOnClickListener(onClickListener);
    }

    @Override
    public void show() {
        super.show();
        setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        checkRadio();
    }

    /**
     * 设置性别
     *
     * @param gender
     */
    public void setGender(int gender) {
        if (gender == GENDER_MALE || gender == GENDER_FEMALE) {
            this.gender = gender;
        }
    }

    /**
     * 设置回掉
     *
     * @param callback
     */
    public void setCallback(Callback callback) {
        this.mCallback = callback;
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.gender_ll_item1) {
                gender = GENDER_MALE;
            } else if (v.getId() == R.id.gender_ll_item2) {
                gender = GENDER_FEMALE;
            }
            checkRadio();
            if (mCallback != null) {
                dismiss();
                mCallback.onGender(gender);
            }
        }
    };


    private void checkRadio() {
        if (gender == GENDER_MALE) {
            if (!mRadioButton1.isChecked()) {
                mRadioButton1.setChecked(true);
                mRadioButton2.setChecked(false);
            }
        } else if (gender == GENDER_FEMALE) {
            if (!mRadioButton2.isChecked()) {
                mRadioButton1.setChecked(false);
                mRadioButton2.setChecked(true);
            }
        }


    }

    /**
     * 回掉接口
     */
    public interface Callback {
        void onGender(int gender);
    }
}
